package generator;
import com.alibaba.fastjson.JSON;

import java.sql.Connection;
import java.sql.DatabaseMetaData;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.ResultSetMetaData;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;  
  
//jdbc链接数据库,获取表名
public class MysqlUtil {




    public static void main(String[] args) throws Exception 
    {  
  
        String url = "jdbc:mysql://127.0.0.1:3306/wzry?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true";
        String user = "root";  
        String password = "123456";  
  
        List<TableInfo> list = getTableInfo(url , user , password);

        System.out.println(JSON.toJSONString(list));

    }

    /**
     * 获取所有表名
     * @param url
     * @param user
     * @param password
     * @return
     */
    public static List<TableInfo> getTableInfo( String url , String user , String password)
    {

        List<TableInfo> list = null;
        Connection conn = null;
        try
        {
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            else
                System.err.println("connect filed");
            // 获取所有表名
            list = getTables(conn);
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            if(conn != null)
            {
                try{conn.close();}catch (Exception e){};
            }
        }

        return list;
    }

    private static List<TableInfo> getTables(Connection conn) throws SQLException
    {  
    	List<TableInfo> list = new ArrayList<TableInfo>();
        DatabaseMetaData dbMetData = conn.getMetaData();  
        // mysql convertDatabaseCharsetType null  
        ResultSet rs = dbMetData.getTables(null,  null , null,  new String[] { "TABLE", "VIEW" });
  
        while (rs.next()) 
        {  
            if (rs.getString(4) != null  && (rs.getString(4).equalsIgnoreCase("TABLE") 
            		|| rs.getString(4).equalsIgnoreCase("VIEW"))) 
            {
                TableInfo tableInfo = new TableInfo();
                List<TableFieldsInfo> tableFieldsInfos = new ArrayList<TableFieldsInfo>();
                String tableName = rs.getString(3).toLowerCase();
                tableInfo.setName(tableName);
                System.out.println(tableName + "\t");
                // 根据表名提前表里面信息：
                ResultSet colRet = dbMetData.getColumns(null, "%", tableName,"%");  
                while (colRet.next()) 
                {
                    TableFieldsInfo tableFieldsInfo = new  TableFieldsInfo();
                    String columnName = colRet.getString("COLUMN_NAME");  
                    String columnType = colRet.getString("TYPE_NAME");  
                    String comment = colRet.getString("REMARKS");
                    int datasize = colRet.getInt("COLUMN_SIZE");
                    int digits = colRet.getInt("DECIMAL_DIGITS");  
                    int nullable = colRet.getInt("NULLABLE");  
                     System.out.println(columnName + " " + columnType + " " + comment + "  " +
                     datasize + " " + digits + " " + nullable);
                    tableFieldsInfo.setColumnName(columnName);
                    tableFieldsInfo.setColumnType(columnType);
                    tableFieldsInfo.setComment(comment);
                    tableFieldsInfo.setSize(datasize);
                    tableFieldsInfos.add(tableFieldsInfo);
                }
                tableInfo.setTableFieldsInfos(tableFieldsInfos);
                list.add(tableInfo);
            }
        }  
        return list;
  
    }



    static class TableInfo
    {
        private String name;
        private List<TableFieldsInfo> tableFieldsInfos;



        public List<TableFieldsInfo> getTableFieldsInfos() {
            return tableFieldsInfos;
        }

        public void setTableFieldsInfos(List<TableFieldsInfo> tableFieldsInfos) {
            this.tableFieldsInfos = tableFieldsInfos;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }


    static class TableFieldsInfo
    {
        private String columnName;
        private String columnType;
        private String comment;
        private Integer size;


        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public String getColumnType() {
            return columnType;
        }

        public void setColumnType(String columnType) {
            this.columnType = columnType;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }

        public Integer getSize() {
            return size;
        }

        public void setSize(Integer size) {
            this.size = size;
        }
    }
}
